package com.medical.wechat.service.impl;

import com.medical.common.CustomizeException;
import com.medical.wechat.mapper.WechatAccountMapper;
import com.medical.wechat.model.WechatAccount;
import com.medical.wechat.service.WechatAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@SuppressWarnings("SpringJavaAutowiringInspection")
@Service("wechatAccountService")
public class WechatAccountServiceImpl implements WechatAccountService {

	@Autowired
	private WechatAccountMapper accountMapper;
	
	public void createWechatAccount(WechatAccount account) {
        Date date=new Date();
//        Timestamp timestamp=null;
//        try {
//             timestamp=new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date.toString()).getTime());
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }

        if (account != null) {
			if(StringUtils.isEmpty(account.getId())){
				account.setId(UUID.randomUUID().toString());
			}
			if(StringUtils.isEmpty(account.getName())){
				throw new CustomizeException("name不能为空");
			}
			if(StringUtils.isEmpty(account.getTokenExpired())){
				account.setTokenExpired(date);
			}
			if(StringUtils.isEmpty(account.getCreatedTime())){
                account.setCreatedTime(date);
            }
			accountMapper.createWechatAccount(account);
		}else{
			throw new CustomizeException("信息不能为空");
		}
	}
	public void updateWechatAccountById(WechatAccount account) {
		if (account != null) {
			accountMapper.updateWechatAccountById(account);
		}else {
			throw new CustomizeException("信息不能为空");
		}
	}

	public List<WechatAccount> findAll() {
		return accountMapper.findAll();
	}

	public WechatAccount findById(String id) {
		return accountMapper.findById(id);
	}

	public void deleteById(String id) {
		accountMapper.deleteById(id);
	}

	@Override
	public List<WechatAccount> findByOrganizationId(String organizationId) {
		return accountMapper.findByOrganizationId(organizationId);
	}

	@Override
	public WechatAccount findByOriginalId(String originalId) {
		return accountMapper.findByOriginalId(originalId);
	}

}
